import { defineStore } from 'pinia'
import {ref, computed} from 'vue'
import axios from 'axios';

export const useCounterStore = defineStore('counter', ()=>{
  // 数据 (state)
  const count = ref(0)

  // 定义修改数据的方法（action 同步+异步）
  const increment = () => {
    count.value++
  }

  // 实现getters
  // getters直接使用计算属性即可实现
  const doubleCount = computed(() => count.value * 2)

  // 异步action
  const API_URL = 'http://geek.itheima.net/v1_0/channels'
  const list = ref([])
  const loadList = async () => {
    const res = await axios.get(API_URL)
    list.value = res.data.data.channels
  }


  // 以对象形式返回
  return {
    count,
    increment,
    doubleCount,
    list,
    loadList
  }
})


